package cn.com.aiouyi.controller.admin;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.aiouyi.common.Message;
import cn.com.aiouyi.entity.Accessory;
import cn.com.aiouyi.entity.Photo;
import cn.com.aiouyi.page.Pageable;
import cn.com.aiouyi.service.AccessoryService;
import cn.com.aiouyi.service.ArticleClassService;
import cn.com.aiouyi.service.ArticleService;
import cn.com.aiouyi.service.PhotoPositionService;
import cn.com.aiouyi.service.PhotoService;
import cn.com.aiouyi.vo.Link;

/**
 * Controller - 导航
 * @author hanson
 *
 */
@Controller("adminPhotoController")
@RequestMapping("/admin/photo")
public class PhotoController extends BaseController{

	@Resource(name = "photoServiceImpl")
	private PhotoService photoService;
	@Resource(name = "photoPositionServiceImpl")
	private PhotoPositionService photoPositionService;
	@Resource(name = "articleClassServiceImpl")
	private ArticleClassService articleClassService;
	@Resource(name = "articleServiceImpl")
	private ArticleService articleService;
	@Resource(name = "accessoryServiceImpl")
	private AccessoryService accessoryService;
	
	@GetMapping("/list")
	public String list(Pageable pageable, ModelMap model){
		
		model.put("page", photoService.findPage(pageable));
		return "/admin/photo/list";
	}
	
	@GetMapping("/add")
	public String add(ModelMap model){
		
		model.put("photoPositions", photoPositionService.findAll());
		model.put("articleClasses", articleClassService.findAll());
		model.put("linkTypes", Link.Type.values());
		return "/admin/photo/add";
	}
	
	@GetMapping("/articleList")
	public String articleList(Pageable pageable, ModelMap model){
		
		model.put("page", articleService.findPage(pageable));
		return "/admin/navigation/articleList";
	}
	
	@PostMapping("/save")
	public String save(@Valid Photo photo, Long photoPositionId, Long accessoryId, RedirectAttributes redirectAttributes){
		photo.setPhotoPosition(photoPositionService.find(photoPositionId));
		Accessory accessory = accessoryService.find(accessoryId);
		if(accessory != null){
			photo.setAccessory(accessory);
		}
		photoService.save(photo);
		
		addFlashMessage(redirectAttributes, Message.SUCCESS);
		return "redirect:list";
	}
	
	@GetMapping("/edit")
	public String edit(Long id, ModelMap model){
		
		model.put("photo", photoService.find(id));
		model.put("photoPositions", photoPositionService.findAll());
		model.put("articleClasses", articleClassService.findAll());
		model.put("linkTypes", Link.Type.values());
		return "/admin/photo/edit";
	}
	
	@PostMapping("/update")
	public String update(Long id, @Valid Photo photo, Long photoPositionId, 
			Long parentId, Long accessoryId, ModelMap model, RedirectAttributes redirectAttributes){
		photo.setPhotoPosition(photoPositionService.find(photoPositionId));
		
		photo.setAccessory(accessoryService.find(accessoryId));
		photoService.update(photo);
		
		addFlashMessage(redirectAttributes, Message.SUCCESS);
		return "redirect:list";
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public Message delete(Long[] ids){
		
		photoService.realDelete(ids);
		
		return Message.SUCCESS;
	}
}
